/*
 * $RCSfile: Processor_46_Msg.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-16  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.i366.processor;

import java.util.Map;

import org.apache.log4j.Logger;

import com.i366.Constant;
import com.i366.cache.Cache;
import com.i366.room.model.RoomPersion;
import com.i366.room.model.impl.Room;
import com.i366.service.RoomService;
import com.work.client.util.I366ClientPickUtil;
import com.work.comm.util.I366PickUtil;
import com.work.db.model.UserInfo;
import com.work.engine.protocal.request.Request;
import com.work.engine.server.handler.Handler;

/**
 *
 * 发送消息
 * <p>Title: Processor_46_Msg</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * @author shenxing.ruan
 * @version 1.0
 */
public class Processor_46_Msg extends Handler{

	private Logger logger = Logger.getLogger(Processor_46_Msg.class);
	public byte[] handleRequest() {
		logger.debug("Processor_46_Msg userId is : " + this.getGameRequest().getUserId());
		//=======================相关验证=================
		Request request = getGameRequest();
		RoomService.updateUser(request);
		int[][] int2 = {{60,I366ClientPickUtil.TYPE_INT_1},
				{132,I366ClientPickUtil.TYPE_INT_4},
				{131,I366ClientPickUtil.TYPE_INT_4},
				{130,I366ClientPickUtil.TYPE_STRING_UTF16}
		};
		Map<Integer, Object> map = I366ClientPickUtil.pickAll(request.getBt(), int2);
		int mgsType = (Integer)map.get(60);
		int roomId = (Integer)map.get(132);
		int roomPath = (Integer)map.get(131);
		try {
			String msgContent = (String)map.get(130);

			UserInfo userInfo = Cache.getOnlineUserInfo().get(request.getUserId());
			Room room = Cache.getRoom(roomId, roomPath);
			RoomPersion roomPersion = room.getAudMap().get(request.getUserId());

			Object[][] objs = {
					{60,roomPersion.getSize(),I366ClientPickUtil.TYPE_INT_1},
					{131,userInfo.getNikeName(),I366ClientPickUtil.TYPE_STRING_UTF16},
					{63,mgsType,I366ClientPickUtil.TYPE_INT_1},
					{130,msgContent,I366ClientPickUtil.TYPE_STRING_UTF16}
			};
			byte[] bytes = I366PickUtil.packAll(objs, Constant.REQ_GAME_RECV_MSG);
			room.getRoomService().send(bytes, request.getUserId());
		} catch (Exception e) {
			logger.debug("发送消息出错", e);
			return pusUser(1);
		}
		return pusUser(0);
	}

	public byte[] pusUser(int status) {
		Object[][] objs2 = {
				{60,status,I366ClientPickUtil.TYPE_INT_1}
		};
		byte[] bytes = I366PickUtil.packAll(objs2, Constant.REQ_GAME_SEND_MSG);
		return bytes;
	}

	public Object makeObject() throws Exception {
		return new Processor_46_Msg();
	}

}

